package com.icow.wms.basic.utils;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

@Slf4j
public class DoorServer {


    public void start(Integer port) throws Exception {

        //服务端要建立两个group，一个负责接收客户端的连接，一个负责处理数据传输
        //连接处理group
        EventLoopGroup boss = new NioEventLoopGroup();
        //事件处理group
        EventLoopGroup work = new NioEventLoopGroup();
        //创建ServerBootstrap实例
        ServerBootstrap b = new ServerBootstrap();
        b.group(boss, work)
                //设置将要被实例化的ServerChannel类
                .channel(NioServerSocketChannel.class)
                //设置服务端用于通信的端口号
                .localAddress(new InetSocketAddress(port))
                //设置处理数据的类
                //IdleStateHandler这个类会根据你设置的超时参数的类型和值，
                //循环去检测channelRead和write方法多久没有被调用了，如果这个时间超过了你设置的值，那么就会触发对应的事件，
                //read触发read，write触发write，all触发all[TimeUnit.SECONDS]单位秒
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) {

                        ch.pipeline().addLast(new IdleStateHandler(5, 2, 2, TimeUnit.SECONDS));

                        ch.pipeline().addLast(new DoorHandler());
                    }
                });
        //绑定端口后，开启监听
        ChannelFuture channelFuture = b.bind().sync();
        if (channelFuture.isSuccess()) {
            log.info("door-tcp-ip server started,port is " + port);
        }
    }


    /*public void connect(String ip, int port) {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            //创建一个EventLoopGroup
            Bootstrap clientBootstrap = new Bootstrap();
            //创建和配置Bootstrap
            clientBootstrap.group(group);
            clientBootstrap.channel(NioSocketChannel.class);
            InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, port);
            //clientBootstrap.remoteAddress(inetSocketAddress);
            //创建一个ChannelInitializer，并把它附件到Bootstrap实例中
            clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
                protected void initChannel(SocketChannel socketChannel) {
                    socketChannel.pipeline().addLast(new LiftHandler());
                }
            });
            //等待直到客户端关闭
            ReconnectHandler reconnectHandler = new ReconnectHandler(
                    clientBootstrap, inetSocketAddress, group.next(), 1000, 3000, 1.5);

            reconnectHandler.connect(); // Start the connection process
        } catch (Exception e) {
            e.printStackTrace();
            log.info("连接发生异常");
        }
    }*/
}
